import time
import traceback
from  selenium.webdriver.support import  expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from autoTest_ecshop.utils.fileUtil  import read
from autoTest_ecshop.utils.log_util import logger


url=read.get_yaml("common.yaml")["url"]
class BasePage():
    def __init__(self,driver):
        self.driver = driver
        self.wait = WebDriverWait(self.driver, 10)


    #查找单个元素
    def find_element(self,locator):
        logger.info(f"查找元素{locator}")
        try:
            element=self.wait.until(EC.visibility_of_element_located(locator))
            logger.info(f"元素{locator} 已找到")
            return  element
        except Exception as e:
            logger.error(f"元素{locator} 查找失败：错误 \n{traceback.format_exc}")

    # 查找多个元素
    def find_elements(self,locators):
        logger.info(f"查找元素{locators}")
        try:
            elements=self.wait.until(EC.visibility_of_elements_located(locators))
            logger.info(f"元素{locators} 已找到")
            return  elements
        except Exception as e:
            logger.error(f"元素{locators} 查找失败：错误 \n{traceback.format_exc}")


    def input_data(self,locator,data):
        logger.info(f"在{locator},输入{data}")
        element = self.find_element(locator)
        element.clear()
        element.send_keys(data)


    def click_d(self,locator):
        logger.info(f"点击{locator}")
        element = self.find_element(locator)
        element.click()
        time.sleep(1)

        #获取文本信息
    def get_text(self,locator):
        logger.info(f"获取元素{locator}的文本")
        element = self.find_element(locator)
        return element.text

    #警告框的输入及文本获取
    def alert_window(self,inputdata="",retrieve_text=True):
            alert = self.driver.switch_to.alert
            if inputdata:
                logger.info(f"在警告框中输入：{inputdata}")  # 输入文本信息到警告框中
                alert.send_keys(inputdata)
            if retrieve_text:
                logger.info("获取警告框文本")  # 获取警告框的文本信息
                return alert.text

    #警告框的确认及取消操作
    def alert_opption(self,accept=True):
            alert= self.driver.switch_to.alert
            if accept:
                logger.info("点击确认按钮")  # 点击确认按钮
                alert.accept()
            else:
                logger.info("点击取消按钮")  # 点击取消按钮
                alert.dismiss()
    def switch_frame(self,locator=None,parent=False,default=False):     #切换表单
        if locator:
            logger.info(f"查找表单{locator}")
            element = self.find_element(locator)
            logger.info(f"切换到表单{locator}")
            try:
                self.driver.switch_to.frame(element)
            except:
                logger.error(f"切换到表单{locator} 失败")
                raise
        if parent:
            logger.info("返回上一层表单")
            self.driver.switch_to.parent_frame()
        if default:
            logger.info("返回到默认表单")
            self.driver.switch_to.default_content()


    def execute_js(self, readonly_locator):
        # 定位readonly属性，删除只读属性
        logger.info(f"执行JS脚本，删除元素{readonly_locator}readonly的值")
        try:
            js_Time = f'document.querySelectorAll({readonly_locator})[0].removeAttribute("readonly")'
            self.driver.execute_script(js_Time)
        except:
            logger.error("执行JS脚本失败，错误信息如下：\n", {traceback.format_exc()})
            raise